package cn.tedu.csmall.product.config;

import jakarta.validation.Validation;
import jakarta.validation.ValidatorFactory;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ValidationConfiguration
 *
 * @author Chen
 * @since 2023/1/3 15:19:19
 */
@Configuration
@Slf4j
public class ValidationConfiguration {

    public ValidationConfiguration() {
        log.debug("创建配置类对象：ValidationConfiguration");
    }

    @Bean
    public jakarta.validation.Validator validator() {
        try (ValidatorFactory factory = Validation.byProvider(HibernateValidator.class)
                .configure() // 开始配置
                .failFast(true) // 配置快速失败
                .buildValidatorFactory()) {
            return factory.getValidator();
        }
    }

}
